//
//  ViewController.m
//  Tommao
//
//  Created by mac on 16-3-10.
//  Copyright (c) 2016年 mac. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (retain, nonatomic) IBOutlet UIImageView *ImageViewCat;
- (IBAction)eat;
- (IBAction)drink;
- (IBAction)knockout;
- (IBAction)fart;
- (IBAction)pie;
- (IBAction)cymbal;
- (IBAction)scratch;
- (IBAction)foot_left;
- (IBAction)foot_right;
- (IBAction)angry;
- (IBAction)stomach;

@end

@implementation ViewController

-(void)startAnimating:(int)count picName:(NSString *)picName
{
    //判断当前动画是否正在执行，如果正在执行该次点击无效
    if (self.ImageViewCat.isAnimating ==1)
    {
        return;
    }
    
    //1.加载图片到帧动画数组中
    NSMutableArray * arrayM = [NSMutableArray array];
    for(int i =0;i<count;i++)
    {
        //拼接图片名字
        NSString * imgName = [NSString stringWithFormat:@"%@_%02d.jpg",picName,i];
        //根据图片名字得到图片
        //UIImage * imgCat = [UIImage imageNamed:imgName];
        NSString * Path = [[NSBundle mainBundle]pathForResource:imgName ofType:nil];
        UIImage * imgCat = [UIImage imageWithContentsOfFile:Path];
        [UIImage imageWithContentsOfFile:Path];
        
        //将每次获取的图片放到数组中去
        [arrayM addObject:imgCat];
    }
    //2.将得到的帧动画数组赋给uiimageview的帧动画数组属性
    self.ImageViewCat.animationImages = arrayM;
    
    //3.设置动画播放时间
    self.ImageViewCat.animationDuration = self.ImageViewCat.animationImages.count * 0.1;
    
    //4.设置动画循环次数
    self.ImageViewCat.animationRepeatCount = 1;
    
    //5.开始播放帧动画
    [self.ImageViewCat startAnimating];
    
    //6.动画播放完之后清空缓存
    //self.ImageViewCat.animationImages = nil;
    [self.ImageViewCat performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:self.ImageViewCat.animationImages.count * 0.1];
    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [_ImageViewCat release];
    [super dealloc];
}



- (IBAction)eat
{
    [self startAnimating:39 picName:@"eat"];
}

- (IBAction)drink
{
    [self startAnimating:80 picName:@"drink"];
}

- (IBAction)knockout
{
    [self startAnimating:80 picName:@"knockout"];
}

- (IBAction)fart
{
    [self startAnimating:27 picName:@"fart"];
}

- (IBAction)pie
{
    [self startAnimating:23 picName:@"pie"];
}

- (IBAction)cymbal
{
    [self startAnimating:12 picName:@"cymbal"];
}

- (IBAction)scratch
{
    [self startAnimating:55 picName:@"scratch"];
}

- (IBAction)foot_left
{
    [self startAnimating:29 picName:@"foot_left"];
}

- (IBAction)foot_right
{
    [self startAnimating:29 picName:@"foot_right"];
}

- (IBAction)angry
{
    [self startAnimating:25 picName:@"angry"];
}

- (IBAction)stomach
{
    [self startAnimating:33 picName:@"stomach"];
}


@end
